package com.lancoo.answer.widget.audioPlayView;

import android.content.Context;
import android.media.MediaPlayer;

import com.lancoo.answer.manager.MediaManager;

public class TopicMediaEngine implements MediaEngine {
    private MediaManager mediaManager;
    private IAudioCallback iAudioCallback;

    public TopicMediaEngine(Context context){
        this.mediaManager = new MediaManager(context);
    }

    @Override
    public void setUpMedia(String audioUrl,IAudioCallback iAudioCallback) {
        this.iAudioCallback = iAudioCallback;
        mediaManager.setUpMedia(audioUrl, new MediaManager.OnPlayListener() {
            @Override
            public void onComplete() {
                pauseTimer();
                if (iAudioCallback!=null){
                    iAudioCallback.reset();
                }
            }

            @Override
            public void onStartError() {
                pauseTimer();
                if (iAudioCallback!=null){
                    iAudioCallback.onError();
                }
            }

            @Override
            public void onPausePlay() {
                pauseTimer();
            }

            @Override
            public void onSourceError() {
                pauseTimer();
                if (iAudioCallback!=null){
                    iAudioCallback.onError();
                }
            }

            @Override
            public void onStopPlay() {
                pauseTimer();
                if (iAudioCallback!=null){
                    iAudioCallback.pause();
                }
            }

            @Override
            public void onPrepare() {
                if (iAudioCallback!=null){
                    iAudioCallback.play();
                    iAudioCallback.onPrepared();

                }
            }

            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                //不能在这里进行更新UI进度操作，有问题
            }
        });
    }

    @Override
    public void start() {
        mediaManager.start();
    }

    @Override
    public void pause() {
        pauseTimer();
        mediaManager.pause();
    }

    @Override
    public boolean isPlaying() {
        return mediaManager.isPlay();
    }

    @Override
    public void seekTo(int progress) {
        mediaManager.seekTo(progress);
    }

    @Override
    public boolean isReadyToPlay() {
        return mediaManager.isPrepared();
    }

    @Override
    public void release() {
        pauseTimer();
        mediaManager.release();
    }

    @Override
    public void speed(float speed) {
//        mediaManager.setPlaySpeed(speed);
    }


    /**
     * 初始化timer
     */
    private void initTimer(){
    }

    /**
     * 销毁上一次使用的Timer
     */
    private void destroyTimer(){
    }

    /**
     * 启动Timer
     */
    private void startTimer(){
        destroyTimer();
        initTimer();
    }
    /**
     * 取消,暂停Timer
     */
    private void pauseTimer(){
    }
}
